# -*- coding: utf-8 -*-

"""
* Copyright (C) 2016 GridSafe, Inc.
"""
import uuid
from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager
from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import cached_property
from django.db import models
from apps.mixin import UserMixin
from easy_thumbnails.fields import ThumbnailerImageField


class Profile(AbstractBaseUser, UserMixin):
    """
    用户信息表
    """

    WOMAN = 'woman'
    MAN = 'man'
    SECRET = 'secret'
    SEX_CHOICE = (
        (MAN, '男'),
        (WOMAN, '女'),
        (SECRET, '保密'),
    )

    id = models.AutoField(primary_key=True)
    user_id = models.CharField(max_length=255, db_index=True)

    email = models.EmailField(verbose_name='Email', max_length=255, unique=True, db_index=True)
    username = models.CharField(max_length=50, blank=False, null=False)
    phone = models.CharField(max_length=255, blank=True, null=True)
    sex = models.CharField(max_length=20, choices=SEX_CHOICE, null=False)
    career = models.CharField(max_length=50, blank=False, null=False)
    photo = ThumbnailerImageField(upload_to='photos', blank=True, null=True, verbose_name=(u'photo'))
    description = models.CharField(max_length=255, blank=True, null=True)

    is_active = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    class Meta:
        db_table = 'profile'

    @cached_property
    def user(self, id):
        try:
            return User.objects.get(id=self.user_id)
        except ObjectDoesNotExist:
            return None

    @property
    def get_full_name(self):
        return self.emall

    @property
    def is_staff(self):
        return self.is_superuser

class UserManager(BaseUserManager):
    """通过邮箱，密码创建用户"""

    def create_user(self, phone, email, username, password=None, type=None, **kwargs):
        email = email or ''
        if (email, phone) is None:
            raise ValueError('必须要有邮箱或者手机号码')

        user = self.model(
            email=UserManager.normalize_email(email),
            phone=phone,
            username=username,
            type=type if type else 0
        )
        user.set_password(password)
        if kwargs:
            if kwargs.get('sex', None): user.sex = kwargs['sex']
            if kwargs.get('is_active', None): user.is_active = kwargs['is_active']
            if kwargs.get('career', None): user.uid = kwargs['career']
            if kwargs.get('desc', None): user.desc = kwargs['desc']
            if kwargs.get('avatar', None): user.avatar = kwargs['avatar']

        user.save(using=self._db)
        return user

    def create_super_user(self, email, password, username):
        """
        created and save superuser with the given email, date of birth and password
        :return:
        """
        user = self.create_user(
            email=email,
            password=password,
            username=username,
            phone=None,
        )
        user.is_superuser = True
        user.save(using=self._db)
        return user
